package com.zxs.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zxs.common.enums.IsDeleteCodeEnum;
import com.zxs.domain.convert.SubjectCategoryConvert;
import com.zxs.common.entity.Result;
import com.zxs.domain.entity.bo.SubjectCategoryBO;
import com.zxs.domain.entity.dto.SubjectCategoryDTO;
import com.zxs.domain.service.SubjectCategoryDomainService;
import com.zxs.infra.basic.entity.SubjectCategory;
import com.zxs.infra.basic.mapper.SubjectCategoryMapper;
import com.zxs.infra.basic.service.SubjectCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zxs
 * @date 2025/11/4 下午9:15
 */
@Service
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    private static final Logger log = LoggerFactory.getLogger(SubjectCategoryDomainServiceImpl.class);
    @Autowired
    private SubjectCategoryService categoryService;

    @Autowired
    private SubjectCategoryMapper subjectCategoryMapper;

    @Override
    public Result addSubjectCategory(SubjectCategoryBO subjectCategoryBO) {
        Result result = new Result();

        if (subjectCategoryBO.getCategoryName() == null || subjectCategoryBO.getCategoryName().equals("")) {
            result.setCode(1);
            result.setSuccess(false);
            result.setMessage("分类名称不能为空！");
            return result;
        }

        LambdaQueryWrapper<SubjectCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SubjectCategory::getCategoryName, subjectCategoryBO.getCategoryName());
        SubjectCategory subjectCategory = subjectCategoryMapper.selectOne(queryWrapper);
        if (subjectCategory != null) {
            //该分类名称已经存在
            result.setCode(1);
            result.setSuccess(false);
            result.setMessage("该分类名称已存在！");
            return result;
        }

        subjectCategory = SubjectCategoryConvert.INSTANCE.convertBoToCategory(subjectCategoryBO);
        log.info("转换前的subjectCategoryBO-----" + subjectCategoryBO);
        log.info("转换后的subjectCategory-----" + subjectCategory);
        subjectCategory.setCreatedTime(new Date());
        subjectCategory.setUpdateTime(new Date());
        subjectCategory.setCreatedBy("zxs");
        int insert = subjectCategoryMapper.insert(subjectCategory);
        if (insert > 0) {
            result.setCode(0);
            result.setSuccess(true);
            return result;
        }
        return result;
    }

    /**
     * 查询岗位分类
     * @return
     */
    @Override
    public List<SubjectCategoryDTO> queryPrimaryCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = new SubjectCategory();
        subjectCategory.setParentId(0);
        subjectCategory.setIsDelete(IsDeleteCodeEnum.UN_DELETED.getCode());
        subjectCategory.setCategoryType(subjectCategoryBO.getCategoryType());
        List<SubjectCategory> subjectCategoryList = categoryService.queryPrimaryCategory(subjectCategory);
        List<SubjectCategoryDTO> subjectCategoryBOList = SubjectCategoryConvert.INSTANCE.convertCategoryListToCategoryDTO(subjectCategoryList);
        log.info("查询岗位大分类集合转换后的数据：{}", JSON.toJSONString(subjectCategoryBOList));
        return subjectCategoryBOList;
    }

    /**
     * 查询二级分类
     * @param subjectCategoryBO
     * @return
     */
    @Override
    public List<SubjectCategoryDTO> querySeconedCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategory.setParentId(subjectCategoryBO.getCategoryId());
        subjectCategory.setIsDelete(IsDeleteCodeEnum.UN_DELETED.getCode());
        subjectCategory.setCategoryType(subjectCategoryBO.getCategoryType());
        List<SubjectCategory> subjectCategoryList = categoryService.queryPrimaryCategory(subjectCategory);
        List<SubjectCategoryDTO> subjectCategoryBOList = SubjectCategoryConvert.INSTANCE.convertCategoryListToCategoryDTO(subjectCategoryList);
        log.info("查询二级分类集合转换后的数据：{}", JSON.toJSONString(subjectCategoryBOList));
        return subjectCategoryBOList;
    }

    /**
     * 修改分类
     * @param subjectCategoryBO
     * @return
     */
    @Override
    public Boolean updateCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategory.setUpdateTime(new Date());
        int i = subjectCategoryMapper.updateById(subjectCategory);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 逻辑删除分类
     * @param subjectCategoryBO
     * @return
     */
    @Override
    public Boolean deleteCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategory.setUpdateTime(new Date());
        subjectCategory.setIsDelete(IsDeleteCodeEnum.DELETED.getCode());
        int i = subjectCategoryMapper.updateById(subjectCategory);
        if (i > 0) {
            return true;
        }
        return false;
    }

}